﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using DiscoveryLogic.DataServices.DataSources;
 


namespace DiscoveryLogic.UI.Layouts.DataAccesses
{
    public class AccountManager
    {
        #region Private Variables

        List<Account> listOfLocalAccounts = null;
        List<Account> listOfWSAccounts = null;
    

        #endregion

        #region Constructors

        public AccountManager()
        {
            // Chris: Commented out WS call
            //this.StartGetAccountsAsync(); 
        }

        #endregion


        #region Public Methods

        // Calls webservice and returns list of Account objects if WS is enabled.
        public List<Account> GetAccounts()
        {
            //JAIMER  hack to get around async WS problem .. 
                        
            if ( ((App)Application.Current).UseWebServices )  
            {
                return this.WSAccounts;
            }
            else
            {
                return this.LocalAccounts;
            }
        }

        private List<Account> LocalAccounts
        {
            get
            {
                if (listOfLocalAccounts == null)
                {
                    listOfLocalAccounts = new List<Account>(LocalDataFiles.LoadData<Account[]>("DiscoveryLogic.UI.Layouts.LocalDataFiles.Accounts.xml"));

                    AdjustDates(listOfLocalAccounts);
                }

                return listOfLocalAccounts;
            }
        }

       
        private List<Account> WSAccounts
        {
            get
            {
                if (listOfWSAccounts == null)
                {
 
                    listOfWSAccounts = AccountService.Accounts ;

                    AdjustDates(listOfWSAccounts);
                }

                return listOfWSAccounts;
            }
        }

        private void AdjustDates(List<Account> list)
        {
            //Adjust dates
            list.ForEach(acc =>
            {
                //Set the last transaction date to 10 days less to now.
                acc.LastTransactionDate = Utils.MakeDateDayOffset(acc.LastTransactionDate, -10);

                //Set the next payment date to be next month keeping the original day
                acc.NextPaymentDueDate = Utils.MakeDateMonthOffset(acc.NextPaymentDueDate, 1);
            });
        }

        // Calls webservice and returns list of Accounts objects of type t
        public List<Account> GetAccounts( DiscoveryLogic.DataServices.DataSources.AccountType t)
        {
            List<Account> accountList = null;

            //if ( (!SL2Samples.DesignTimeHelper.IsInDesignMode)  &&  ((App)(Application.Current)).UseWebServices )
            //{
            //    accountList = this.WSAccounts;
            //}
            //else
            //{
            //    accountList = this.LocalAccounts;
            //}
            accountList = this.LocalAccounts;
            // Filter through and get accounts of type t
            List<Account> accounts = new List<Account>();

            // For each account
            for (int i = 0; i < accountList.Count; i++)
            {
                // Take accounts matching type t
                if (accountList[i].Type == t)
                {
                    accounts.Add(accountList[i]);
                }
            }

            return accounts;
        }

         

        #endregion


       

    }
}
